// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © oquant

//@version=6
indicator("Normalized WMA Oscillator | Oquant")
import TradingView/ta/10 as ta


//inputs

src = input.source(close, "Source", group = "Source")
wmalen = input.int(20, "Wma Length", group = "Wma normalization")
normalizationlen = input.int(30, "Min-Max Length", group = "Wma normalization")
upperthreshold = input.float(0.7, "Upper Threshold", step = 0.05, group = "Thresholds")
lowerthreshold = input.float(0.5, "Lower Threshold", step = 0.05, group = "Thresholds")

//wma

wma = ta.wma(src, wmalen)

//min-max normalization

minwma = ta.lowest(wma, normalizationlen)
maxwma = ta.highest(wma, normalizationlen)

rangewma = maxwma - minwma
normalization = (wma - minwma)/rangewma

//signals

long = normalization > upperthreshold
short = normalization < lowerthreshold

var oquant = 0

if long and not short 
    oquant := 1

if short 
    oquant := -1


//plotting

oquantgreen = color.rgb(31, 211, 37)
oquantpurple = color.rgb(188, 8, 219)

colors = oquant==1 ? oquantgreen : oquant==-1 ? oquantpurple : na

plot(normalization, "Plot normalization", colors)
plot(upperthreshold, "Plot Upper Threshold", oquantgreen)
plot(lowerthreshold, "Plot Lower Threshold", oquantpurple)
plotcandle(open, high, low, close, "Plot Bars", colors, colors, bordercolor = colors, force_overlay = true)

alertcondition(ta.crossover(oquant, 0), "Normalized WMA Oscillator Long", "Normalized WMA Oscillator Long {{exchange}}:{{ticker}}")
alertcondition(ta.crossunder(oquant, 0), "Normalized WMA Oscillator Short", "Normalized WMA Oscillator Short {{exchange}}:{{ticker}}")
